import { MessagePlugin } from "tdesign-vue-next";
import { ref } from "vue";

import { toFile } from "@/libs/utils/dataTreating";

import {
  delApi,
  getApi,
  getFileApi,
  getImportTemplateApi,
  postApt,
  putApi,
} from "./api";
import { formData } from "./constants";
import { DataType } from "./type";

export const data = ref<any>([
  {
    id: "1",
    name: "一等奖",
    time: "2025年4月29日",
    type: "普通",
    status: "线上比赛",
  }
]);
export const dataLoading = ref(false);
export const searchValue = ref("");
export const filterValue = ref({});
export const saveLoading = ref(false);
export const pagination = ref({
  pageSize: 10,
  total: 0,
  current: 1,
  pageSizeOptions: [5, 10, 15, 20, 30, 50],
});

export const fetchData = async () => {
  dataLoading.value = true;
  try {
    const info = await getApi({
      current: pagination.value.current,
      pageSize: pagination.value.pageSize,
      search: searchValue.value,
      ...filterValue.value,
    });
    const { list } = info;
    data.value = list;
    pagination.value.total = info.total;
  } catch (e) {
    console.error(e);
  } finally {
    dataLoading.value = false;
  }
};

export const postData = async (
  handleClose?: { (): void; (): void },
  __data?: DataType
) => {
  saveLoading.value = true;
  try {
    const requestData: DataType =
      __data || JSON.parse(JSON.stringify(formData.value));
    await postApt(requestData);
    MessagePlugin.success("新增记录成功");
    fetchData();
    handleClose();
    saveLoading.value = false;
  } catch (e) {
    console.error(e);
  } finally {
    saveLoading.value = false;
    dataLoading.value = false;
  }
};

export const putData = async (
  handleClose: { (): void; (): void },
  __data?: DataType
) => {
  saveLoading.value = true;
  try {
    const requestData: DataType =
      __data || JSON.parse(JSON.stringify(formData.value));
    await putApi(requestData);
    MessagePlugin.success("修改记录成功");
    fetchData();
    handleClose();
    saveLoading.value = false;
  } catch (e) {
    console.error(e);
  } finally {
    saveLoading.value = false;
    dataLoading.value = false;
  }
};

export const delData = async (requestData: DataType) => {
  saveLoading.value = true;
  try {
    await delApi(requestData);
    MessagePlugin.success("删除记录成功");
    fetchData();
    saveLoading.value = false;
  } catch (e) {
    console.error(e);
  } finally {
    saveLoading.value = false;
    dataLoading.value = false;
  }
};

export const getFile = async (pageName: string | unknown) => {
  try {
    const result = await getFileApi({
      current: pagination.value.current,
      pageSize: pagination.value.pageSize,
      search: searchValue.value,
      ...filterValue.value,
    });
    toFile(result, `${pageName}.xlsx`);
  } catch (e) {
    console.error(e);
  } finally {
    dataLoading.value = false;
  }
};

export const importTemplate = async (pageName: string | unknown) => {
  try {
    const result = await getImportTemplateApi();
    toFile(result, `${pageName}模版.xlsx`);
  } catch (e) {
    console.error(e);
  } finally {
    dataLoading.value = false;
  }
};
